<?php

function main()
{
    $url = 'http://huobi.bjtwtx.com/job/switch/?hash=davon@jiang';
    $content = @file_get_contents($url);
    echo $content;

    $json = json_decode($content, true);
    if ($json['count'] > 100 && $json['btcusdt'] > 0) {
        return;
    }

    $ini_config = __DIR__ . DIRECTORY_SEPARATOR . 'last.ini';
    $last_time = @file_get_contents($ini_config);

    if (empty($last_time)) {
        $last_time = time();
    } else {
        if (time() - intval($last_time) < 60 * 31) {
            // 锁定当前请求结果,超时时间为31分钟
            return;
        }
    }
    echo time() - intval($last_time);

    file_put_contents($ini_config, time());
//        $command = "cd D:\mowhere\huobi\source\webSocket\src\HuobiWS && php ws_run.php stop && php ws_run.php start & ";
//    $command = "cd /data/wwwroot/huobi_WebSocket/src/HuobiWS && php ws_run.php stop && php ws_run.php start && php ws_run.php stop 5min && php ws_run.php start 5min";
    $command = "cd /data/wwwroot/huobi_WebSocket/src/HuobiWS && php ws_run.php stop && php ws_run.php start";
    echo $command;
// 异步启用后台处理进程
    if ($resource = popen($command, 'r')) {
        while (!feof($resource)) {
            $buffer = fgets($resource, 4096);
            echo $buffer;
//            printf("%s\r\n", $buffer);
        }
        pclose($resource);
    } else {
        echo 'stop error';
    }
}

main();
